package com.zhongfl.crm.service.impl.boss;

import com.zhongfl.crm.lixiao.mapper.OrderDefaultAssistUserMapper;
import com.zhongfl.crm.service.boss.OrderDefaultAssistUserService;
import com.zhongfl.crm.model.boss.OrderDefaultAssistUser;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.heptagram.channel.api.common.AssertUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 订单默认的协作人
 * @author: 蜻蜓
 * @date: 2019-01-15 19:53:36
 */
@Service
public class OrderDefaultAssistUserServiceImpl implements OrderDefaultAssistUserService {

    @Resource
    private OrderDefaultAssistUserMapper orderDefaultAssistUserMapper;


    /**
     * 保存
     *
     * @param orderDefaultAssistUser
     */
    @Override
    public void save(OrderDefaultAssistUser orderDefaultAssistUser) {
        AssertUtils.notNull(orderDefaultAssistUser,"订单默认的协作人对象为空!");
        orderDefaultAssistUserMapper.insert(orderDefaultAssistUser);
    }

    /**
     * 查询
     *
     * @return
     */
    @Override
    public List<OrderDefaultAssistUser> findAll() {
        return orderDefaultAssistUserMapper.selectAll();
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        orderDefaultAssistUserMapper.deleteById(id);
    }

    /**
     * 保存
     *
     * @param orderAssistUserList
     * @param currentName
     * @param currentStaffCode
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(List<OrderDefaultAssistUser> orderAssistUserList, String currentName, String currentStaffCode) {
        if(CollectionUtils.isNotEmpty(orderAssistUserList)){
            orderAssistUserList.stream().forEach(e -> {
                OrderDefaultAssistUser user = orderDefaultAssistUserMapper.selectByStaffCode(e.getStaffCode());
                if(Objects.isNull(user)){
                    OrderDefaultAssistUser  defaultAssistUser = new OrderDefaultAssistUser();
                    defaultAssistUser.setStaffCode(e.getStaffCode());
                    defaultAssistUser.setStaffName(e.getStaffName());
                    defaultAssistUser.setAmibaId(100000);
                    defaultAssistUser.setAmibaName("协同组");
                    defaultAssistUser.setCreatedBy(currentName);
                    defaultAssistUser.setModifiedBy(currentName);
                    orderDefaultAssistUserMapper.insert(defaultAssistUser);
                }
            });
        }
    }
}
